﻿using System.Linq;
using Dyn.News.Models;
using Dyn.News.Services;
using Dyn.News.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Routable.Models;

namespace Dyn.News.Drivers {
    public class CategoryArchivesPartDriver : ContentPartDriver<CategoryArchivesPart> {
        private readonly ICategoryervice _Categoryervice;
        private readonly IArticleService _ArticleService;
        private readonly IContentManager _contentManager;

        public CategoryArchivesPartDriver(
            ICategoryervice Categoryervice, 
            IArticleService ArticleService,
            IContentManager contentManager) {
            _Categoryervice = Categoryervice;
            _ArticleService = ArticleService;
            _contentManager = contentManager;
        }

        protected override DriverResult Display(CategoryArchivesPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_Category_CategoryArchives",
                                () => {
                                    NewsCategoryPart Category = GetCategoryFromSlug(part.ForCategory);

                                    if (Category == null)
                                        return null;

                                    return shapeHelper.Parts_Category_CategoryArchives(ContentItem: part.ContentItem, Category: Category, Archives: _ArticleService.GetArchives(Category));
                                });
        }

        protected override DriverResult Editor(CategoryArchivesPart part, dynamic shapeHelper) {
            var viewModel = new CategoryArchivesViewModel {
                Slug = part.ForCategory,
                Category = _Categoryervice.Get().ToList().OrderBy(b => b.Name)
                };

            return ContentShape("Parts_Category_CategoryArchives_Edit",
                                () => shapeHelper.EditorTemplate(TemplateName: "Parts.Category.CategoryArchives", Model: viewModel, Prefix: Prefix));
        }

        protected override DriverResult Editor(CategoryArchivesPart part, IUpdateModel updater, dynamic shapeHelper) {
            var viewModel = new CategoryArchivesViewModel();
            if (updater.TryUpdateModel(viewModel, Prefix, null, null)) {
                part.ForCategory = viewModel.Slug;
            }

            return Editor(part, shapeHelper);
        }

        protected override void Importing(CategoryArchivesPart part, ImportContentContext context) {
            var Categorylug = context.Attribute(part.PartDefinition.Name, "Categorylug");
            if (Categorylug != null) {
                part.ForCategory = Categorylug;
            }
        }

        protected override void Exporting(CategoryArchivesPart part, ExportContentContext context) {
            context.Element(part.PartDefinition.Name).SetAttributeValue("Categorylug", part.ForCategory);
        }

        private NewsCategoryPart GetCategoryFromSlug(string slug) {
            return _contentManager.Query<NewsCategoryPart, NewsCategoryPartRecord>()
                .Join<RoutePartRecord>().Where(rr => rr.Slug == slug)
                .List().FirstOrDefault();            
        }
    }
}